<?php
namespace Admin\Controller;

use Think\Controller;
use Think\Model;


class SearchController extends BaseController {

    /**
     * 采购商品列表
     */
    public function index() {
        $p = max(I('param.p'), 1);
        $num = max(I('param.num'), 20);
        $this->assign('p', $p);
        $this->assign('num', $num);

        $order_field = I('param.order_field', 'id');
        $order = I('param.order', 'desc');
        $this->assign('order_field', $order_field);
        $this->assign('order', $order);
        $sunxu = $order == 'asc' ? 'desc' : 'asc';
        $this->assign('order', $sunxu);

        $order = array($order_field => $order);

        $search_field = I('param.search_field', 'name');
        $search = I('param.search');
        $where[] = ' 1=1 ';
        $local_id = session('userInfo')['local_id'];
        $where[] = array('local_id' => $local_id);
        if ($search && $search_field) {
            $where[] = $search_field." like '%".$search."%'";
            $this->assign('search_field', $search_field);
            $this->assign('search', $search);
        }

        $count = M('store_search')->where($where)->count();
        if ($count > 0) {
            $Page = new \Think\Page($count, $num);
            $show = $Page->show();

            $list = M('store_search')
                ->where($where)
                ->order($order)
                ->page($p, $num)
                ->select();
            foreach ($list as &$item) {
                if($item['keywords']) {
                    $temp = json_decode($item['keywords'], true);
                    $item['keywords'] = join(',', $temp);
                }
            }
            $this->assign('list', $list);
            $this->assign('page', $show);
        }
        $this->display();
    }

    public function add() {
        $this->display('edit');
    }

    public function edit() {
        if (IS_POST) {
            $id = I('post.id');
            $save['name'] = I('post.name', '', 'htmlspecialchars');
            $save['state'] = (bool)I('post.state') ? '1': '0';
            $keywords = array_filter(I('post.keywords'));
            sort($keywords);
            $save['keywords'] = json_encode($keywords);
            if ($id) {
                $res = M('store_search')->where(['id'=>$id])->save($save);
            } else {
                $save['local_id'] = session('userInfo')['local_id'];
                $res = M('store_search')->add($save);
            }
            if ($res !== false) {
                redirect(U('index'));
            } else {
                $this->error('错误');
            }
            exit();
        }
        $id = I('get.id');
        if (!$id) {
            $this->error('缺少参数');
        }
        $info = M('store_search')->where(['id'=>$id])->find();
        $info['keywords'] = json_decode($info['keywords'], true);
        $info['num'] = count($info['keywords']);
        $this->assign('info', $info);
        $this->display('edit');
    }

    /**
     * 修改关键字状态 开启/关闭
     */
    public function change() {
        if (!IS_AJAX) {
            $this->error('错误的请求方式');
        }
        $id = I('get.id');
        $state = I('get.state');
        $state = $state == 'true' ? '1': '0';
        $res = M('store_search')->where(['id'=>$id])->save(['state'=>$state]);
        if ($res !== false) {
            $this->ajaxReturn(['status'=>true, 'message'=>'成功', 'data'=>'']);
        } else {
            $this->ajaxReturn(['status'=>false, 'message'=>'失败', 'data'=>'']);
        }
    }

    public function delete() {
        if (!IS_AJAX) {
            $return = array(
                'status' => false,
                'message' => '非法请求',
            );
            $this->ajaxReturn($return);
        }
        $ids = I('post.id');
        //dump($ids);die;
        if (is_array($ids)) {
            $ids = join(',', $ids);
        }
        $res = M('store_search')->delete($ids);
        if ($res !== false) {
            $return = array(
                'status' => true,
                'message' => '成功',
            );
        } else {
            $return = array(
                'status' => false,
                'message' => '失败',
            );
        }
        $this->ajaxReturn($return);
    }

}